﻿using Newtonsoft.Json;

namespace NLS.API
{
    public static class Extension
    {
        public static DateTime GetDateTime(this int timeStamp)
        {
            long lTime = ((long)timeStamp * 10000000);
            TimeSpan toNow = new TimeSpan(lTime);
            DateTime targetDt = new DateTime(1970, 1, 1).Add(toNow);
            return targetDt;
        }

        public static string? ToJson(this object? obj)
        {
            try
            {
                return JsonConvert.SerializeObject(obj);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"{ex.Message}\r\n{ex.Source}\r\n{ex.StackTrace}");
                return null;
            }
        }

        public static T? ToObject<T>(this string content)
        {
            try
            {
                return JsonConvert.DeserializeObject<T>(content);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"{ex.Message}\r\n{ex.Source}\r\n{ex.StackTrace}");
                return default(T?);
            }
        }
    }
}
